/***********************************************************************
 * Module:  Rating.java
 * Author:  Voja
 * Purpose: Defines the Class Rating
 ***********************************************************************/

package model.entity;

import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "rating")
@NamedQuery(name = "isUserRate", 
	query = "SELECT rating FROM Rating rating WHERE rating.user.id=:userid AND rating.photo.id = :photoid")
public class Rating implements Serializable{

	private static final long serialVersionUID = -240005125929231401L;

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "rating_id", unique = true, nullable = false)
    private Integer id;
	
	@Column(name = "rating_vote", unique = false, nullable = true)
    private Integer vote;
   
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false)
    private User user;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name = "photo_id", referencedColumnName = "photo_id", nullable = false)
    private Photo photo;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getVote() {
		return vote;
	}

	public void setVote(Integer vote) {
		this.vote = vote;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public String toString() {
		return "Rating [id=" + id + "]";
	}
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof Rating))
			return false;
		return ((Rating)obj).getId().intValue()==getId().intValue();
	}
}